package com.link.base.base.contract.controller;

import com.link.base.base.contract.model.ModContract;
import com.link.base.base.contract.service.ModContractService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 价格模具合同
 *
 * @author 阳晓琳
 * @date 2020-10-13 14:08:15
 */
@Controller
@RequestMapping("/link/modContract")
public class ModContractController extends BasicController<ModContract> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private ModContractService modContractService;

    @Override
    public BasicService<ModContract> getBasicService() throws Exception {
        return modContractService;
    }

    /**
     * 价格模具合同失效
     * @author 王昊
     * @param entity
     * @param session
     * @param request
     * @return
     */
    @RequestMapping(value = "/unactive",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> unactiveContract(@JsonParam ModContract entity, HttpSession session, HttpServletRequest request) {
        Map<String,Object> result = new HashMap<>(4);
        try {
            modContractService.unactiveContract(entity);
            result.put("newRow",entity);
            result.put("success",true);
        } catch (Exception e) {
            result.put("success",false);
            result.put("result",e.getMessage());
        }
        return result;
    }

    /**
     * 价格模具合同审批
     * @author 王昊
     * @param entity
     * @param session
     * @param request
     * @return
     */
    @RequestMapping(value = "/priceModSubmit" , method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> priceModSubmit(@JsonParam ModContract entity, HttpSession session, HttpServletRequest request) {
        Map<String,Object> result = new HashMap<>(4);
        try {
            modContractService.priceModSubmit(entity);
            result.put("newRow",entity);
            result.put("success",true);
        } catch (Exception e) {
            result.put("success",false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/change",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> change(@JsonParam ModContract entity, HttpSession session, HttpServletRequest request) {
        Map<String,Object> result = new HashMap<>(4);
        try {
            if (null == entity.getId()) {
                throw new RuntimeException("id不能为空!");
            }
            entity = modContractService.queryById(entity.getId());
            if (!"Effect".equals(entity.getContractStatus())) {
                throw new RuntimeException("当前合同状态[" + entity.getContractStatus() + "]不能变更!");
            }
            modContractService.changeContractStatusToNew(entity);
            result.put("newRow",entity);
            result.put("success",true);
        } catch (Exception e) {
            result.put("success",false);
            result.put("result",e.getMessage());
        }
        return result;
    }
}
